{extend name="public:base"}
{block name='css'}
<link href="__PUBLIC__/css/plugins/iCheck/custom.css" rel="stylesheet">
{/block}
{block name="js"}
<script src="__PUBLIC__/js/plugins/peity/jquery.peity.min.js"></script>
<script src="__PUBLIC__/js/content.min.js?v=1.0.0"></script>
<script type="text/javascript">

    function delAll(self){
        if ($(self).hasClass('ed')) {
            return false;
        }

        layer.confirm('真的要删除吗？删除后将无法找回！', {
            btn: ['确定','取消'] //按钮
        }, function(){
            myRequestRefresh(self, "{:url('delAllCate')}", $("#delRecord").serialize());
        }, function(){

        });
    }

    function del(self, id){
        if ($(self).hasClass('ed')) {
            return false;
        }

        layer.confirm('真的要删除吗？删除后将无法找回！', {
            btn: ['确定','取消'] //按钮
        }, function(){
            myRequestRefresh(self, "{:url('delCate')}", {id:id});
        }, function(){

        });
    }

    function sort(self){
        if ($(self).hasClass('ed')) {
            return false;
        }

        myRequestRefresh(self, "{:url('saveCateSort')}", $("#delRecord").serialize());
    }

    $(function(){
        //全选的实现
        $(".check-all").click(function(){
            $(".ids").prop("checked", this.checked);
        });
        $(".ids").click(function(){
            var option = $(".ids");
            option.each(function(i){
                if(!this.checked){
                    $(".check-all").prop("checked", false);
                    return false;
                }else{
                    $(".check-all").prop("checked", true);
                }
            });
        });

    });
    
    function searchCate() {
        var pid = $('select[name=pid]').val();
        var keywords = $.trim($('input[name=keywords]').val());

        if (pid > 0) {
            $('tbody tr').hide();
            $('.pid-'+pid).show();
            if (keywords != '') {
                $('.pid-' + pid).each(function (i, n) {
                    if (eval('/'+keywords+'/').test($(n).find('td').eq(2).text())) {
                        $(n).show();
                    } else {
                        $(n).hide();
                    }
                });
            }
        } else {
            $('tbody tr').show();
            if (keywords != '') {
                $('tbody tr').each(function (i, n) {
                    if (eval('/'+keywords+'/').test($(n).find('td').eq(2).text())) {
                        $(n).show();
                    } else {
                        $(n).hide();
                    }
                });
            }
        }
    }
    function resetSearch() {
        $('select[name=pid] option').eq(0).prop('selected', true);
        $('input[name=keywords]').val('');
        searchCate();
    }
</script>
{/block}
{block name='body'}
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>分类列表</h5>
                    <a href="{:url('cate_save')}" class="btn btn-sm btn-primary pull-right m-t-n-xs">添加分类</a>
                </div>
                <div class="ibox-content">
                    <div class="row col-sm-12">
                        <div class="col-sm-1 m-b-xs">
                            <input type="submit" class="btn btn-sm btn-primary btn-sm btn-danger pull-left" onclick="delAll(this);return false;" value="删除"  />
                        </div>

                        <div class="col-sm-1">
                            <button type="button" onclick="window.location.reload();" id="loading-example-btn" class="btn btn-white btn-sm"><i class="fa fa-refresh"></i> 刷新</button>
                        </div>

                        <div class="col-sm-10 m-b-xs">
                            <div class="col-sm-4 m-b-xs">
                                <select name="pid" class="input-sm  form-control">
                                    <option value="">上级分类</option>
                                    {foreach name="list" item="li"}
                                    <option value="{$li.id}" {if condition="isset($li.id) && $li.id eq input('pid')"}selected{/if}>{$li.title}</option>
                                    {/foreach}
                                </select>
                            </div>
                            <div class="col-sm-6">
                                <div class="input-group pull-right">
                                    <input type="text" name="keywords" value="{:input('get.keywords')}" placeholder="分类名称" class="input-sm form-control"> <span class="input-group-btn">
                                    <button type="submit" onclick="searchCate();" class="btn btn-sm btn-primary"> 搜索</button>
                                    <a href="javascript:;" onclick="resetSearch();" class="btn btn-sm btn-warning"> 重置</a> </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <form name="form" action=""  method="post" id="delRecord">
                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>
                                    <input class="check-all" type="checkbox" value="">
                                </th>
                                <th>上级分类</th>
                                <th>名称</th>
                                <th>图片</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>

                            {empty name="list"}
                            <tbody><tr><td colspan="10" style="text-align: center;">暂未查询到分类信息~</td></tr></tbody>
                            {else/}
                            <tbody>
                            {foreach name="list" item="li"}
                            <tr class="pid-{$li.id}">
                                <td><input type="checkbox" class="ids" value="{$li.id}" name="id[]"></td>
                                <td>{$li.ptitle|default='-'}</td>
                                <td>{$li.title}</td>
                                <td><img src="{$li.img}" height="80"/></td>
                                <td><input type="number"  style="width:60px;text-align:center;" name="{$li.id}" value="{$li.sort}" class="input-sm form-control"></td>
                                <td>
                                    <a href="{:url('cate_save',['id'=>$li['id']])}" class="btn btn-success btn-xs edit"><i class="fa fa-edit"></i> 编辑</a>
                                    <a href="javascript:;" onclick="del(this, {$li.id});return false;" class="btn btn-danger btn-xs delete"><i class="fa fa-trash-o"></i> 删除</a>
                                </td>
                            </tr>
                            {notempty name='li.list'}
                            {foreach name="li.list" item="son"}
                            <tr class="pid-{$son.pid}">
                                <td><input type="checkbox" class="ids" value="{$son.id}" name="id[]"></td>
                                <td>{$son.ptitle|default='-'}</td>
                                <td>{$son.title}</td>
                                <td><img src="{$son.img}" height="80"/></td>
                                <td><input type="number"  style="width:60px;text-align:center;" name="{$son.id}" value="{$son.sort}" class="input-sm form-control"></td>
                                <td>
                                    <a href="{:url('cate_save',['id'=>$son['id']])}" class="btn btn-success btn-xs edit"><i class="fa fa-edit"></i> 编辑</a>
                                    <a href="javascript:;" onclick="del(this, {$son.id});return false;" class="btn btn-danger btn-xs delete"><i class="fa fa-trash-o"></i> 删除</a>
                                </td>
                            </tr>
                            {/foreach}
                            {/notempty}
                            {/foreach}
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="4">&nbsp;</td>
                                <td>
                                    <button onclick="sort(this);return false;" class="btn btn-sm btn-primary m-t-n-xs" />修改排序</button>
                                </td>
                                <td></td>
                            </tr>
                            </tfoot>
                            {/empty}


                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>

</div>

</body>
{/block}
